package com.jiang.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jiang.vo.Area;

import net.sf.json.JSONObject;

@WebServlet(name="jsonDemo", urlPatterns="/jsonDemo")
public class JsonDemo extends HttpServlet {

	private static final long serialVersionUID = 1L;

	/**
		 * The doGet method of the servlet. <br>
		 *
		 * This method is called when a form has its tag value method equals to get.
		 * 
		 * @param request the request send by the client to the server
		 * @param response the response send by the server to the client
		 * @throws ServletException if an error occurred
		 * @throws IOException if an error occurred
		 */
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		List<Area> areas = new ArrayList<Area>();
		for(int i=0;i<10;i++){// 我是分支测试 
			Area area = new Area();
			area.setAid(1000+i);
			area.setTitle("中国省份"+(i+1));
			areas.add(area);
		}
		JSONObject jsonObj = new JSONObject();
		jsonObj.put("provinces", areas);
		// 因为JSON就是一个字符串，前端就无忧了
		response.getWriter().print(jsonObj.toString());
	}

	/**
		 * The doPost method of the servlet. <br>
		 *
		 * This method is called when a form has its tag value method equals to post.
		 * 
		 * @param request the request send by the client to the server
		 * @param response the response send by the server to the client
		 * @throws ServletException if an error occurred
		 * @throws IOException if an error occurred
		 */
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		this.doGet(request, response);
	}

}
